﻿using System.Collections.Generic;
using System.Linq;
using Comm.DAL;
using Comm.Model;
using System.Data;
using System;
using System.IO;
using System.Web;
using System.Text;

namespace Comm.BLL 
{
	public partial class BooksService
	{
   		     
		/// <summary>
        /// 数据库操作对象
        /// </summary>
        private BooksDAO _dao = new BooksDAO();

        #region 向数据库中添加一条记录 +int Insert(Books model)
        /// <summary>
        /// 向数据库中添加一条记录
        /// </summary>
        /// <param name="model">要添加的实体</param>
        /// <returns>插入数据的ID</returns>
        public int Insert(Books model)
        {
            return _dao.Insert(model);
        }
        #endregion

        #region 删除一条记录 +int Delete(int id)
        public int Delete(int id)
        {
            return _dao.Delete(id);
        }
        #endregion

        #region 根据主键ID更新一条记录 +int Update(Books model)
        /// <summary>
        /// 根据主键ID更新一条记录
        /// </summary>
        /// <param name="model">更新后的实体</param>
        /// <returns>执行结果受影响行数</returns>
        public int Update(Books model)
        {
            return _dao.Update(model);
        }
        #endregion

        #region 分页查询一个集合 +IEnumerable<Books> QueryList(int index, int size, object wheres, string orderField, bool isDesc = true)
        /// <summary>
        /// 分页查询一个集合
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="size">页大小</param>
        /// <param name="wheres">条件匿名类</param>
        /// <param name="orderField">排序字段</param>
        /// <param name="isDesc">是否降序排序</param>
        /// <returns>实体集合</returns>
        public IEnumerable<Books> QueryList(int index, int size, object wheres, string orderField, bool isDesc = true)
        {
            return _dao.QueryList(index, size, wheres, orderField, isDesc);
        }
        #endregion

        #region 查询单个模型实体 +Books QuerySingle(object wheres)
        /// <summary>
        /// 查询单个模型实体
        /// </summary>
        /// <param name="wheres">条件匿名类</param>
        /// <returns>实体</returns>
        public Books QuerySingle(object wheres)
        {
            return _dao.QuerySingle(wheres);
        }
        #endregion
        
        #region 查询单个模型实体 +Books QuerySingle(int id)
        /// <summary>
        /// 查询单个模型实体
        /// </summary>
        /// <param name="id">key</param>
        /// <returns>实体</returns>
        public Books QuerySingle(int id)
        {
            return _dao.QuerySingle(id);
        }
        #endregion

        #region 查询条数 +int QueryCount(object wheres)
        /// <summary>
        /// 查询条数
        /// </summary>
        /// <param name="wheres">查询条件</param>
        /// <returns>实体</returns>
        public int QueryCount(object wheres)
        {
            return _dao.QueryCount(wheres);
        }
        #endregion

        //-------------------------------------------------------------
        /// <summary>
        /// 获取指定类别下的总的页数
        /// </summary>
        /// <param name="categoryId"></param>
        /// <param name="pageSize">每页显示的记录数</param>
        /// <returns></returns>
        public int GetPageCount(int categoryId, int pageSize)
        {
            int recordCount = _dao.GetRecordCount(categoryId);
            int pageCount = Convert.ToInt32(Math.Ceiling((double)recordCount / pageSize));
            return pageCount;
        }

        public int GetRowCount() 
        {
            int recordCount = _dao.GetRecordCount(0);
            return recordCount;
        }

        /// <summary>
        /// 获取指定类别下的总的页数
        /// </summary>
        /// <param name="categoryId"></param>
        /// <param name="pageSize">每页显示的记录数</param>
        /// <returns></returns>
        public int GetPageCount1(int categoryId, int pageSize,string bName)
        {
            int recordCount = _dao.GetRecordCount1(categoryId, bName);
            int pageCount = Convert.ToInt32(Math.Ceiling((double)recordCount / pageSize));
            return pageCount;
        }

        /// <summary>
        /// 获取分页的数据
        /// </summary>
        /// <param name="pageIndex">当前的页码</param>
        /// <param name="pageSize"></param>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public IList<Model.Books> GetPageData(int pageIndex, int pageSize, int categoryId, string orderby,string search)
        {
            int start = (pageIndex - 1) * pageSize + 1;//计算开始位置
            int end = pageIndex * pageSize;//终止位置
            return _dao.GetPageData(start, end, categoryId, orderby, search);

        }

        /// <summary>
        /// 将指定书的内容生成静态页
        /// </summary>
        /// <param name="bookId"></param>
        public string CreateStaticPage(int bookId,string type)
        {        
            string htmlPath = "";
            Model.Books model = _dao.GetModel(bookId);
            StringBuilder sb = new StringBuilder();
            if (type == "true")
            {
                if (model != null)
                {
                    sb.Append("<html><head><title>" + model.BooksList[0].Title + "</title></head>");
                    sb.Append("<body>");
                    sb.Append("<table>");
                    sb.Append("<tr><td style=\"width:120px;\">书名:</td><td>" + model.BooksList[0].Title + "</td></tr>");
                    sb.Append("<tr><td style=\"width:120px;\">作者:</td><td>" + model.BooksList[0].Author + "</td></tr>");
                    sb.Append("<tr><td style=\"width:120px;\">价格:</td><td>" + model.BooksList[0].UnitPrice.ToString("0.00") + "</td></tr>");
                    sb.Append("<tr><td style=\"width:120px;\">封面:</td><td><img src='../Images/BookCovers/" + model.BooksList[0].ISBN + ".jpg'/></td></tr>");
                    sb.Append("<tr><td style=\"width:120px;\">内容:</td><td>" + model.BooksList[0].ContentDescription + "</td></tr>");
                    sb.Append("</table>");
                    sb.Append("</body></html>");
                    htmlPath = "../StaticPage/" + model.BooksList[0].Id + ".html";
                    if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(htmlPath))) {
                        File.WriteAllText(System.Web.HttpContext.Current.Server.MapPath("../StaticPage/" + model.BooksList[0].Id + ".html"), sb.ToString(), Encoding.UTF8);                   
                    }
                }
            }
            else {
                //模板  
                string templapate = File.ReadAllText(HttpContext.Current.Server.MapPath("../Template/BookTemplate.html"));
                string html = templapate.Replace("$title", model.BooksList[0].Title).Replace("$body", model.BooksList[0].ContentDescription).Replace("$bookId", model.BooksList[0].Id.ToString());

                string dir = HttpContext.Current.Server.MapPath("../StaticPage/" + model.BooksList[0].PublishDate.Year + "/" + model.BooksList[0].PublishDate.Month + "/" + model.BooksList[0].PublishDate.Day + "/");
                Directory.CreateDirectory(Path.GetDirectoryName(dir));//创建文件夹
                htmlPath = "../StaticPage/" + model.BooksList[0].PublishDate.Year + "/" + model.BooksList[0].PublishDate.Month + "/" + model.BooksList[0].PublishDate.Day + "/" + model.BooksList[0].Id + ".html";
                if (!File.Exists(HttpContext.Current.Server.MapPath(htmlPath)))
                {
                  File.WriteAllText(dir + model.BooksList[0].Id + ".html", html, Encoding.UTF8);//将内容写到文件
                }
            }
            return htmlPath;
        }
        public IList<Books> GetBooksList() {
            return _dao.GetBooksList();
        }
	}
}